#include "rigist.h"
Rigist::Rigist()
{
    setupUi(this);
    connect(pushButton,SIGNAL(clicked()),this,SLOT(rigist()));
    this->setWindowTitle(tr("注册"));
    lineEdit_2->setEchoMode(QLineEdit::Password);
    lineEdit_3->setEchoMode(QLineEdit::Password);
}
void Rigist::rigist()
{
      int ctl_sock=tcp_connect(SRV_IP,SRV_CTL_PORT);
           if(ctl_sock<0)
               return;

       QString q_usr = lineEdit->text();
       QString q_pwd_1 = lineEdit_2->text();
       QString q_pwd_2 = lineEdit_3->text();

       string usr = q_usr.toStdString();
       string pwd_1 = q_pwd_1.toStdString();
       string pwd_2 = q_pwd_2.toStdString();

       //检验两次密码是否相同
       if( pwd_1 != pwd_2 )
       {
           QErrorMessage err_msg(this);
           err_msg.setWindowTitle("Error:");
           err_msg.showMessage(tr("您两次输入的密码名不相同"));
           err_msg.exec();

           ::close(ctl_sock);
           return;
       }

       //打包
       ctl_pkg_t info;
       info.type = REG;
       strncpy(info.str.id.usr,usr.c_str(),USR_LEN-1);
       strncpy(info.str.id.pwd,pwd_1.c_str(),PWD_LEN-1);


       //发送给服务器
       if( writen(ctl_sock,&info,sizeof(ctl_pkg_t)) < 0 )
       {
           QErrorMessage err_msg(this);
           err_msg.setWindowTitle(tr("错误"));
           err_msg.showMessage(tr("无法连接到服务器"));
           err_msg.exec();

           ::close(ctl_sock);
           return;
       }

       //读取服务器的应答
       int8_t reply;
       if( readn(ctl_sock,&reply,sizeof(reply)) < 0 )
       {
           QErrorMessage err_msg(this);
           err_msg.setWindowTitle(tr("错误"));
           err_msg.showMessage(tr("无法连接到服务器"));
           err_msg.exec();

           ::close(ctl_sock);
           return;
       }

       if( reply == -1 )
       {
           QErrorMessage err_msg(this);
           err_msg.setWindowTitle(tr("错误:"));
           err_msg.showMessage(tr("该用户名已经被注册，请您换一个用户名"));
           err_msg.exec();

           ::close(ctl_sock);
           return;
       }

       QMessageBox::about(NULL,tr("注册成功"),tr("注册成功，您现在可以使用该用户名登录"));
       this->close();
}
